﻿using UnityEngine;
using System.Collections;

public class EnemyPlane : Plane
{

	//修饰符 数据类型  变量名
	private Rigidbody2D rig;
	//飞机的移动速度
	public float speed = 5f;
	//子弹预制体
	public GameObject bulletPrefab;
	//发射子弹的声音片段
	public AudioClip shootSond;
	//声音播放器
	private AudioSource audioSource;
	//发射子弹的频率
	public float shootInterval=1f;

	protected override void  Start()
	{
		base.Start ();
		//获取刚体组件
		rig = GetComponent<Rigidbody2D> ();

		audioSource = gameObject.AddComponent<AudioSource> ();
		rig.velocity = Vector2.down * speed;//设置一个速度让飞机向下运动
		if(bulletPrefab !=null){
			InvokeRepeating ("Shoot",Random.Range(0f,1f),shootInterval);
		}else{
			rig.AddTorque(Random.Range(0,2)==0 ? 100:-100);
		}
}
	//发射一颗子弹
	void Shoot()
	{
		Instantiate (bulletPrefab,transform.position,transform.rotation);
		audioSource.PlayOneShot (shootSond);//播放声音
	}
}
